﻿// Decompiled with JetBrains decompiler
// Type: DynamicClipping.CUiDynDynamicClippingCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.DynamicClipping;
using System.Collections.Generic;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DynamicClipping;

public sealed class CUiDynDynamicClippingCmd : TxToggleButtonCommand
{
  private bool m_active;
  private CApDynDynamicClippingEnabler m_enabler;

  public CUiDynDynamicClippingCmd()
  {
    this.m_enabler = new CApDynDynamicClippingEnabler();
    ((TxButtonCommand) this).SetStayEnabledInSemiModalMode(true);
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    foreach (TxGraphicViewer graphicViewer in TxApplication.ViewersManager.GraphicViewers)
      graphicViewer.DynamicClipping.IsEnabled = false;
    this.m_active = false;
  }

  public virtual bool Push => this.m_active;

  public virtual string Tooltip => DynamicClippingStringTable.CMD_TOOLTIP;

  public virtual string Category => DynamicClippingStringTable.CMD_CATEGORY;

  public virtual void Execute(object cmdParams)
  {
    List<TxGraphicViewer> graphicViewers = TxApplication.ViewersManager.GraphicViewers;
    if (!this.m_active)
    {
      TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
      TxApplication.ViewersManager.ViewerAdded += new TxViewersManager_ViewerAddedEventHandler(this.ViewersManager_ViewerAdded);
      this.m_active = true;
    }
    else
    {
      TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
      TxApplication.ViewersManager.ViewerAdded -= new TxViewersManager_ViewerAddedEventHandler(this.ViewersManager_ViewerAdded);
      this.m_active = false;
    }
    int clippingPlaneRatio = CUiDynDynamicClippingApp.GetClippingPlaneRatio();
    foreach (TxGraphicViewer txGraphicViewer in graphicViewers)
    {
      txGraphicViewer.DynamicClipping.IsEnabled = this.m_active;
      if (this.m_active)
        txGraphicViewer.DynamicClipping.ClippingPlaneRatio = (float) clippingPlaneRatio / 100f;
    }
    TxApplication.RefreshDisplay();
  }

  private void ViewersManager_ViewerAdded(object sender, TxViewersManager_ViewerAddedEventArgs args)
  {
    if (!(args.Viewer is TxGraphicViewer))
      return;
    ((TxGraphicViewer) args.Viewer).DynamicClipping.IsEnabled = this.m_active;
    if (!this.m_active)
      return;
    ((TxGraphicViewer) args.Viewer).DynamicClipping.ClippingPlaneRatio = TxApplication.ViewersManager.GraphicViewer.DynamicClipping.ClippingPlaneRatio;
  }

  public virtual string Name => DynamicClippingStringTable.CMD_NAME;

  public virtual string Description => DynamicClippingStringTable.CMD_DESCRIPTION;

  public virtual string Bitmap => "DynamicClipping.DynamicClippingMode.bmp";

  public virtual string LargeBitmap => "DynamicClipping.DynamicClippingMode_LARGE.png";

  public virtual bool Licensed => TxCommandLicenser.DynamicClippingLicense;

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_enabler;

  public virtual ITxImage Image
  {
    get => (ITxImage) TxImageManager.GetImageByKey("DynamicClippingPlaneOnOff");
  }
}
